package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.dto.SetmealDTO;
import com.itheima.dto.SetmealPageQueryDTO;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile imgFile){
        log.info("文件上传:{}", imgFile.getOriginalFilename());
        try {
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取文件后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件
            String objName = UUID.randomUUID().toString() + extension;
            //文件访问路径
            String filePath = aliOssUtil.upload(imgFile.getBytes(), objName);
            //存入redis中
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,objName);
            return Result.success(filePath,MessageConstant.UPLOAD_SUCCESS);
        } catch (IOException e) {
            log.error("文件上传失败:{}", e.getMessage());
        }
        return Result.error(MessageConstant.PIC_UPLOAD_FAIL);
    }
    /**
     * 新增套餐
     */
    @PostMapping("/add")
    public Result add(@RequestBody SetmealDTO setmealDTO,@RequestParam List<String> checkgroupIds){
        log.info("新增的数据为:{}", setmealDTO);
        log.info("新增的关联表的数据id:{}", checkgroupIds);
        setmealService.saveWithCheckGroup(setmealDTO,checkgroupIds);
        return Result.success("新增套餐成功");
    }
    /**
     * 编辑
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody SetmealDTO setmealDTO,@RequestParam List<String> checkgroupIds){
        log.info("编辑的数据为:{}", setmealDTO);
        log.info("编辑的关联表的数据id:{}", checkgroupIds);
        setmealService.updateWithCheckItem(setmealDTO,checkgroupIds);
        return Result.success("编辑套餐成功");
    }
    /**
     * 根据id查询
     */
    @GetMapping("/findById")
    public Result<Setmeal> findById(@RequestParam String id) {
        log.info("查询套餐的id:{}", id);
        Setmeal setmeal=setmealService.findById(id);
        return Result.success(setmeal,"查询检查组成功");
    }
    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询的条件:{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return pageResult;
    }

}
